// Package controller @author dingrui @since 2021/11/28
package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"

	"github.com/bannirui/mini-prize/common"
	"github.com/bannirui/mini-prize/dal/model"
	"github.com/bannirui/mini-prize/dal/service"
	"github.com/bannirui/mini-prize/web/util/response"
)

func AdminUserRouter(party iris.Party) {
	var (
		party_     = party.Party("/admin-user")
		controller = AdminUserController{}
	)
	party_.Get("/", hero.Handler(controller.Get))
	party_.Get("/black", hero.Handler(controller.GetBlack))
}

type AdminUserController struct {
}

func (c *AdminUserController) Get(ctx iris.Context) {
	var (
		prevPage int
		nextPage int
		dataList []model.User
	)
	page := ctx.URLParamIntDefault("page", 1)
	size := ctx.URLParamIntDefault("size", 10)
	// 数据列表
	dataList = service.G_userService.GetAll(page, size)
	total := (page - 1) + len(dataList)
	// 数据总数
	if len(dataList) >= size {
		total = service.G_userService.CountAll()
		nextPage = page + 1
	}
	if page > 1 {
		prevPage = page - 1
	}
	response.Ok(ctx, response.CODE_OK, &response.PageUserVO{
		Datalist: dataList,
		Total:    total,
		Now:      common.NowUnix(),
		PagePrev: prevPage,
		PageNext: nextPage,
	})
	return
}

func (c *AdminUserController) GetBlack(ctx iris.Context) {
	var (
		id  int
		err error
		t   int
	)
	if id, err = ctx.URLParamInt("id"); err != nil {
		goto ERR
	}
	// 封禁天数
	t = ctx.URLParamIntDefault("time", 0)
	if t > 0 {
		t = t*86400 + common.NowUnix()
	}

	if err = service.G_userService.Update(&model.User{Id: id, BlackExpireTime: t, GmtUpdated: common.NowUnix()},
		[]string{"black_expire_time"}); err != nil {
		goto ERR
	}
	response.Ok(ctx, response.CODE_OK, nil)
	return
ERR:
	response.InternalServerError(ctx, response.CODE_SYS_PARSE_PARAMS_ERROR)
	return
}
